using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProvider;
namespace DalatCooking.Core
{
    class ContactAccessor : IContactRepository
    {
        public override bool Insert(Contact entity)
        {
            object rs = IDataProvider.Instance.OExecuteNonQuery("@ContactID", "Contact_Insert", entity.ContactID, entity.FirstName, entity.LastName, entity.CompanyName, entity.Address, entity.MainPhone, entity.Email, entity.Subject, entity.Body);
            return int.Parse(rs.ToString()) > 0;
        }
        public override bool Update(Contact entity)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Contact_Update", entity.ContactID, entity.FirstName, entity.LastName, entity.CompanyName, entity.Address, entity.MainPhone, entity.Email, entity.Subject, entity.Body);
            return rs > 0;
        }
        public override bool Delete(int id)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Contact_Delete", id) > 0;
        }
        public override bool Delete(Contact entity)
        {
            return Delete(entity.ContactID);
        }
        public override List<Contact> FindAll()
        {
            return CBO.FillCollection<Contact>(IDataProvider.Instance.ExecuteReader("Contact_FindAll"));
        }
        public override Contact FindById(int id)
        {
            return CBO.FillObject<Contact>(IDataProvider.Instance.ExecuteReader("Contact_FindById", id));
        }
        public override Contact Single(int id)
        {
            return FindById(id);
        }
        public override Contact FindByName(string name)
        {
            throw new NotImplementedException();
        }
        public override List<Contact> PagingSql(int pageSize, int pageNum, out int totalRowCount)
        {
            IDataReader reader = null;
            reader = IDataProvider.Instance.ExecuteReader("Contact_Paging", pageSize, pageNum);
            reader.Read();
            totalRowCount = reader.GetInt32(0);
            reader.NextResult();
            return CBO.FillCollection<Contact>(reader);
        }
        public override List<Contact> PagingLinq(int pageSize, int pageNum, out int totalRowCount)
        {
            var query = from c in FindAll()
                        select c;
            totalRowCount = FindAll().Count;
            var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
            return list;
        }
        public override bool SetIsRead(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Contact_SetIsRead", id);
            return rs > 0;
        }

        public override bool SetActive(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Contact_SetActive", id);
            return rs > 0;
        }
        public override bool SetSortIndex(int id, int isup)
        {
            return IDataProvider.Instance.ExecuteNonQuery("Contact_SetSortIndex", id, isup) > 0;
        }
        public override bool RemoveSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Contact_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RemoveAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Contact_RemoveAll");
            return rs > 0;
        }
        public override bool RestoreSelectedItem(int id)
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Contact_RemoveSelectedItem", id);
            return rs > 0;
        }
        public override bool RestoreAll()
        {
            int rs = IDataProvider.Instance.ExecuteNonQuery("Contact_RestoreAll");
            return rs > 0;
        }
    }
}